package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.Encrypt;
import com.example.lotterysystem.dao.dataobject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_Xiaochi
 * Date: 2025/8/1
 * Time: 21:22
 */
@Mapper
public interface UserMapper {

    /**
     * 查询邮箱绑定的人数
     * @param email
     * @return
     */
    @Select("select count(*) from user where email = #{email}")
    int countByMail(@Param("email") Encrypt email);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user (user_name, email, phone_number, password, identity) " +
            "values (#{userName}, #{email}, #{phoneNumber}, #{password}, #{identity})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // 此注解可以使数据库中生成的id赋值给userDo中的id
    void insert(UserDO userDo);


    @Select("select * from user where email = #{email}")
    UserDO selectByEMail(@Param("email") Encrypt email);

    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByMobile(@Param("phoneNumber") Encrypt phoneNumber);

    @Select("<script> " +
            "select * from user " +
            "<if test=\"identity!=null\">" +
            "   where identity = #{identity}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);

    // 传入 1 2 3，数据库中只存在 1 和 2，通过这个sql返回的就是 1和2
    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> ids);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);
}
